<template>
	<view>
		<!-- 提现账户管理-支付宝 -->
		<!-- #ifdef APP-PLUS -->
		<nav-bar :title="titleList[type]" leftIcon="back" @click-left="back">
			<block slot="right"><button class="cu-btn bg-blue margin-right-sm" @tap="save" :disabled="btndisabled">保存</button></block>
		</nav-bar>
		<!-- #endif -->
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">{{ addressTitle }}</view>
			<input type="text" v-model="address" :placeholder="addressInfo" placeholder-class="text-gray text-df" />
		</view>
		<view class="cu-form-group padding-lr" v-if="type == 0">
			<view class="title" style="width: 150rpx;">姓名</view>
			<input type="text" v-model="name" placeholder="为了及时提现成功，请填入真实姓名" placeholder-class="text-gray text-df" />
		</view>
		<view class="cu-form-group padding-lr" v-if="type != 0">
			<view class="title" style="width: 150rpx;">备注</view>
			<input type="text" v-model="remarks" placeholder="为了方便区分收款地址，请填入容易识别的备注(选填)" placeholder-class="text-gray text-df" />
		</view>
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">提现密码</view>
			<input type="text" password v-model="payPassword" placeholder="为保证资金账号安全，请输入提现密码" placeholder-class="text-gray text-df" />
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<userButton @submitEvent="save" :btnDisabled="btndisabled">保存</userButton>
		<!-- #endif -->
		<loading :loadModal="loadModal"></loading>
	</view>
</template>

<script>
import userButton from '@/components/user-button.vue';
var _self; //this容器
export default {
	components: {
		userButton
	},
	data() {
		return {
			type: 0, //0：支付宝，1：BTC，2:ETH
			titleList: ['提现账户管理-支付宝', '提现账户管理-BTC', '提现账户管理-ETH'],
			currencyList: ['CNY', 'BTC', 'ETH'],
			id: '',
			name: '',
			address: '',
			remarks: '',
			payPassword: '',
			loadModal: false,
			source: 'u' //返回来源 u 用户设置页，w 钱包提现页
		};
	},
	computed: {
		btndisabled() {
			if (this.address.length > 0 && this.payPassword.length > 5) {
				if (this.type == 0) {
					if (this.name.length > 0) {
						return false;
					}
				} else {
					return false;
				}
			}
			return true;
		},
		addressInfo() {
			return this.type == 0 ? '为了及时提现成功，请填入可用支付宝账号' : '为了及时提现成功，请填入可用收款地址';
		},
		addressTitle() {
			return this.type == 0 ? '支付宝' : '收款地址';
		}
	},
	onLoad(options) {
		_self = this;
		const tmpcnt = options.cnt;
		const tmptype = options.type;
		_self.source = options.s;
		if (tmpcnt) {
			let tmpcontent = JSON.parse(decodeURIComponent(tmpcnt));
			_self.id = tmpcontent.id;
			_self.name = _self.source == 'w' ? tmpcontent.remarks : tmpcontent.name;
			_self.address = tmpcontent.address;
			_self.remarks = tmpcontent.remarks;
		}
		if (tmptype) {
			_self.type = parseInt(tmptype);
		}
	},
	onReady() {
		uni.setNavigationBarTitle({
			title: this.titleList[this.type]
		});
	},
	methods: {
		/* 取消设置 */
		back() {
			uni.navigateBack({ delta: 1 });
		},

		/* 设置用户名 */
		save() {
			let tmpremarks = _self.remarks == null ? '支付宝账号' : _self.remarks;
			const param = {
				id: _self.id,
				currency: _self.currencyList[_self.type],
				name: _self.name,
				address: _self.address,
				remarks: tmpremarks,
				payPassword: _self.payPassword
			};
			_self.loadModal = true;
			_self.$http.httpRequest('userWithdrawalAddress/addOrUpdate', param, true).then(res => {
				_self.loadModal = false;
				if (res._tranStatus) {
					if (_self.source == 'u') {
						_self.$onFire.fire(
							'withdrawListener',
							_self.id,
							encodeURIComponent(_self.name),
							encodeURIComponent(_self.address),
							encodeURIComponent(_self.remarks),
							_self.type
						);
					}
					_self.back();
				} else {
					let info = res._fieldErrors ? '请先设置提现密码' : res._exceptionLocalizedMessage;
					_self.$util.msg(info);
				}
			});
		}
	}
};
</script>
